{gt} package for makeing good looking table

Tech
Author

Tony Duan

Published

October 31, 2022

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
Warning: package 'purrr' was built under R version 4.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(gt)
Warning: package 'gt' was built under R version 4.2.2
#library(palmerpenguins)
islands_tbl <- 
  tibble(
    name = names(islands),
    size = islands
  ) %>%
  arrange(desc(size)) %>%
  slice(1:5)

islands_tbl
# A tibble: 5 × 2
  name           size
  <chr>         <dbl>
1 Asia          16988
2 Africa        11506
3 North America  9390
4 South America  6795
5 Antarctica     5500
gt_tbl <- 
# change DT to GT
  islands_tbl %>%gt() %>%
# add title and subtitle
  tab_header(
    title = "Large Landmasses of the World",
    subtitle = "The top ten largest are presented"
# add foot note
  ) %>%tab_source_note(
    source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
  ) %>%
  tab_source_note(
    source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
  )
#######################


# Show the gt Table
gt_tbl
Large Landmasses of the World
The top ten largest are presented
name size
Asia 16988
Africa 11506
North America 9390
South America 6795
Antarctica 5500
Source: The World Almanac and Book of Facts, 1975, page 406.
Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley.

Appendix:

{gt} with Rich Iannone:

https://www.youtube.com/watch?v=WfPEvRb92mg

Rich Iannone Making Beautiful Tables with {gt} RStudio: https://www.youtube.com/watch?v=z0UGmMOxl-c

gt package by rstudio

https://gt.rstudio.com/ https://gt.rstudio.com/articles/intro-creating-gt-tables.html